package com.ctshk.app.order.localfun.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.req.pay.NotifyConsumeReq;
import com.ctshk.rpc.localfun.req.LocalFunProductOrderReq;
import com.ctshk.rpc.order.localfun.dto.*;
import com.ctshk.rpc.order.localfun.req.*;
import com.ctshk.rpc.order.localfun.req.h5.LocalFunH5OrderReq;
import com.ctshk.rpc.order.localfun.req.h5.LocalFunOrderAppCallPayReq;
import com.ctshk.rpc.order.localfun.service.ILocalFunAppOrderService;
import com.ctshk.rpc.order.localfun.service.ILocalFunPCOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * <p>
 * 当地玩乐订单 前端控制器
 * </p>
 *
 * @author gzl-wansi001
 * @since 2021-02-24
 */
@Api(value="当地玩乐-订单", tags="当地玩乐-订单")
@RestController
@RequestMapping("/localFun/order")
@Slf4j
public class LocalFunAppOrderController extends BaseController {

    @DubboReference
    private ILocalFunAppOrderService iLocalFunAppOrderService;

    @DubboReference
    private ILocalFunPCOrderService iLocalFunPCOrderService;

    @ApiOperation(value = "APP-核对订单", notes = "当地玩乐-填写订单页-核对订单", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "预订中心-填写订单页-核对订单", operDesc = "APP当地玩乐下单")
    @PostMapping(value = "/app-checkOrder")
    public Result<Long> submit(@RequestBody LocalFunH5OrderReq req){
        return iLocalFunAppOrderService.submit(req,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value = "APP-当地玩乐-订单完成-回调", notes = "当地玩乐-支付页面-支付", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "当地玩乐-支付页面-支付", operDesc = "APP当地玩乐支付")
    @PostMapping(value = "/app/add/order")
    public Result<Long> payOrder(@RequestBody @Valid NotifyConsumeReq notifyConsumeReq){
        return iLocalFunAppOrderService.payOrder(notifyConsumeReq,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value = "APP-取消订单（待付款状态）", notes = "取消订单（待付款状态）", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "取消订单（待付款状态）", operDesc = "APP当地玩乐待付款取消订单）")
    @PostMapping(value = "/app/cancel-localFun-order")
    public Result<Long> cancelLocalFunOrder(@RequestBody @Valid LocalFun0rderCancelReq localFun0rderCancelReq){
        return iLocalFunAppOrderService.cancelLocalFunOrder(localFun0rderCancelReq,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value="APP-已支付取消订单", notes="待确认、确认成功、确认失败状态取消订单", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "取消订单", operDesc = "APP当地玩乐已付款取消订单")
    @PostMapping("/app/cancel-refund-order")
    public Result<Long> cancelLocalFunOrderRefund(@RequestBody @Valid AppLocalFunOrderRefundDetailReq localFun0rderRefundDetailReq) {
        return iLocalFunAppOrderService.cancelLocalFunOrderRefund(localFun0rderRefundDetailReq,getCurrentUserAllowNotLogInUserId());
    }


    @ApiOperation(value = "APP-订单详情", notes = "订单中心景点门票详情", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心当地玩乐详情", operDesc = "APP当地玩乐-订单API接口-订单详情")
    @PostMapping(value = "/app/orderDetail")
    public Result<LocalFunOrderDetail> localFunOrderDetail(@RequestBody LocalFunOrderDetailReq localFunOrderDetailReq){
        return iLocalFunAppOrderService.orderDetail(localFunOrderDetailReq,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value = "APP-订单中心-唤起支付", notes = "当地玩乐-订单中心-唤起支付", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "当地玩乐-订单中心-唤起支付", operDesc = "APP当地玩乐-订单API接口-唤起支付")
    @PostMapping("/app/callPay")
    public void callPay(@RequestBody @Valid LocalFunOrderAppCallPayReq callPayReq, HttpServletResponse response) {
        Result result = iLocalFunAppOrderService.callPay(callPayReq);
        log.info("【当地玩乐APP服务-唤起支付】返回html,data{}", result);
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().write(String.valueOf(result.getData()));
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "APP-订单中心-查询支付结果", notes = "当地玩乐-订单中心-查询支付结果", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "APP当地玩乐-订单中心-查询支付结果", operDesc = "APP当地玩乐-订单中心-查询支付结果")
    @GetMapping("/app/pay/result")
    public Result<AppQueryPayResultDTO> queryPayResult(@RequestParam("id") Long id) {
        return iLocalFunAppOrderService.queryPayResult(id);
    }

    @ApiOperation(value = "APP-订单中心-删除订单（仅已取消订单可删除）", notes = "当地玩乐-订单中心-删除订单（仅已取消订单可删除）", tags="APP-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "当地玩乐-订单中心-删除订单（仅已取消订单可删除）", operDesc = "机场快线-订单中心-删除订单（仅已取消订单可删除）")
    @GetMapping(value = "/delete/{orderId}")
    public Result<Long> deleteCancelOrder(@PathVariable Long orderId){
        return iLocalFunAppOrderService.deleteCancelOrder(orderId);
    }
    /*----------------------------------PC订单相关---------------------------------------*/

    @ApiOperation(value = "PC-核对订单", notes = "当地玩乐-填写订单页-核对订单", tags="PC-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "预订中心-填写订单页-核对订单", operDesc = "PC-当地玩乐-订单-下单")
    @PostMapping(value = "/pc-checkOrder")
    public Result pcCheckOrder(@RequestBody LocalFunAppOrderReq req){
        return iLocalFunPCOrderService.pcCheckOrder(req,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value = "PC-当地玩乐-订单支付", notes = "当地玩乐-支付页面-支付", tags="PC-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "当地玩乐-支付页面-支付", operDesc = "PC-当地玩乐-订单-支付")
    @PostMapping(value = "/pc/add/order")
    public Result pcPayOrder(@RequestBody @Valid NotifyConsumeReq notifyConsumeReq){
        return iLocalFunPCOrderService.pcPayOrder(notifyConsumeReq,getCurrentUserAllowNotLogInUserId());
    }


    @ApiOperation(value = "PC-取消订单（待付款状态）", notes = "取消订单（待付款状态）", tags="PC-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "取消订单（待付款状态）", operDesc = "PC-当地玩乐-订单-取消订单（待付款状态）")
    @PostMapping(value = "/pc/cancel-localFun-order")
    public Result<Long> cancelLocalFunOrderPC(@RequestBody @Valid LocalFun0rderCancelReq localFun0rderCancelReq){
        return iLocalFunPCOrderService.cancelLocalFunOrder(localFun0rderCancelReq,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value="PC-取消订单（已支付）", notes="待确认、确认成功、确认失败状态取消订单", tags="PC-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "取消订单", operDesc = "PC-当地玩乐-订单-下单取消订单(已付款)")
    @PostMapping("/pc/cancel-refund-order")
    public Result<Long> cancelLocalFunOrderRefundPC(@RequestBody @Valid AppLocalFunOrderRefundDetailReq localFun0rderRefundDetailReq) {
        return iLocalFunPCOrderService.cancelLocalFunOrderRefund(localFun0rderRefundDetailReq,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value = "PC-订单详情", notes = "订单中心详情", tags="PC-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心当地玩乐详情", operDesc = "PC-当地玩乐-订单-订单详情")
    @PostMapping(value = "/pc/orderDetail")
    public Result<LocalFunOrderDetail> orderDetail(@RequestBody LocalFunOrderDetailReq localFunOrderDetailReq){
        return iLocalFunPCOrderService.orderDetail(localFunOrderDetailReq);
    }

    @ApiOperation(value = "PC-订单中心-唤起支付", notes = "当地玩乐-订单中心-唤起支付", tags="PC-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "当地玩乐-订单中心-唤起支付", operDesc = "PC-当地玩乐-订单-唤起支付")
    @PostMapping("/pc/callPay")
    public void PcCallPay(@RequestBody @Valid LocalFunOrderAppCallPayReq callPayReq, HttpServletResponse response) {
        Result result = iLocalFunPCOrderService.callPay(callPayReq);
        log.info("【当地玩乐APP服务-唤起支付】返回html,data{}", result);
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().write(String.valueOf(result.getData()));
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "PC-订单中心-查询支付结果", notes = "当地玩乐-订单中心-查询支付结果", tags="PC-订单API接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "当地玩乐-订单中心-查询支付结果", operDesc = "PC-当地玩乐-订单-查询支付结果")
    @GetMapping("/pc/pay/result")
    public Result<AppQueryPayResultDTO> queryPcPayResult(@RequestParam("id") Long id) {
        return iLocalFunPCOrderService.queryPayResult(id);
    }

}





















